package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.constant.DubboMethodConstant;
import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntDiscountSurplusQuotaQueryRequest;
import cc.rengu.igas.bsps.facade.response.MchntDiscountSurplusQuotaQueryResponse;
import cc.rengu.igas.momp.facade.base.Header;
import cc.rengu.igas.momp.facade.bean.MchntFeeDiscountMcStatInfoBean;
import cc.rengu.igas.momp.facade.bean.MchntFeeDiscountMchntStatInfoBean;
import cc.rengu.igas.momp.facade.request.QueryMchnFeeDiscountDetailRequest;
import cc.rengu.igas.momp.facade.response.QueryMchnFeeDiscountDetailResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户优惠剩余额度查询.
 *
 * @author zhangxuran (联调修改)
 * @version 1.0
 * @date 2020.05.11
 */
public class MchntDiscountSurplusQuotaQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntDiscountSurplusQuotaQueryRequest mchntDiscountSurplusQuotaQueryRequest = new MchntDiscountSurplusQuotaQueryRequest();
        ConvertUtil.convertOutput(mchntDiscountSurplusQuotaQueryRequest);
        return mchntDiscountSurplusQuotaQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<MchntDiscountSurplusQuotaQueryResponse> bizResponse = new BizResponse<>();
        MchntDiscountSurplusQuotaQueryResponse mchntDiscountSurplusQuotaQueryResponse = new MchntDiscountSurplusQuotaQueryResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        MchntDiscountSurplusQuotaQueryRequest mchntDiscountSurplusQuotaQueryRequest = (MchntDiscountSurplusQuotaQueryRequest) request;
        String instId = mchntDiscountSurplusQuotaQueryRequest.getHeader().getInstId();
        String mchntNo = mchntDiscountSurplusQuotaQueryRequest.getMchntNo();
        String startDate = mchntDiscountSurplusQuotaQueryRequest.getStartDate();
        String endDate = mchntDiscountSurplusQuotaQueryRequest.getEndDate();
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("必填性信息校验失败");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(instId, mchntNo);
        if (null == mchntBaseInfo) {
            rglog.error("获取用户信息失败");
            throw new BizException(RespCodeEnum.QUERY_MCHNT_BASE_INFO_ERROR.getRespCode(), RespCodeEnum.QUERY_MCHNT_BASE_INFO_ERROR.getRespDesc());
        }
        //  重新组装报文头
        QueryMchnFeeDiscountDetailRequest mompRequest = new QueryMchnFeeDiscountDetailRequest();
        Header header = new Header();
        header.setVersion("1.0.0");
        header.setTransDate(DateUtil.getCurrentDate());
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTxnNum("T00MO010");
        header.setBizType("00");
        header.setInstId(instId);
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(ChannelEnum.BSPS.getChannelType());
        header.setTraceNo(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        rglog.info(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        mompRequest.setHeader(header);
        List<MchntFeeDiscountMchntStatInfoBean> mchntInfoList = new ArrayList<>();
        MchntFeeDiscountMchntStatInfoBean mchntFeeDiscountMchntStatInfoBean = new MchntFeeDiscountMchntStatInfoBean();
        mchntFeeDiscountMchntStatInfoBean.setMchntNo(mchntNo);
        mchntInfoList.add(mchntFeeDiscountMchntStatInfoBean);
        mompRequest.setMchntInfoList(mchntInfoList);
        //查询类型 1-商户人员
        mompRequest.setQueryType("1");
        //查询主体
        mompRequest.setQueryId(mchntNo);
        //查询主体所属机构
        mompRequest.setQueryOrgId(mchntBaseInfo.getSignInstId());
        //查询主体  10-商户
        mompRequest.setQueryRole("10");
        mompRequest.setStartDate(startDate);
        mompRequest.setEndDate(endDate);
        mompRequest.setPageIndex("-1");
        mompRequest.setPageSize("-1");

        DubboService dubboService = new DubboServiceImpl();
        Result<QueryMchnFeeDiscountDetailResponse> acctDubboResult = (Result<QueryMchnFeeDiscountDetailResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_MOMP_STA_SERVICE, "queryMchnFeeDiscountDetail", mompRequest);
        if (null == acctDubboResult || !acctDubboResult.isSuccess() || null == acctDubboResult.getResult() || !BspsAppParamConstant.DUBBO_SUCCESS.equals(acctDubboResult.getResult().getRespCode())) {
            bizResponse.setRespCode(null == acctDubboResult ? RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode() : acctDubboResult.getResult().getRespCode());
            bizResponse.setRespDesc(null == acctDubboResult ? RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc() : acctDubboResult.getResult().getRespDesc());
            mchntDiscountSurplusQuotaQueryResponse.setRespDesc(null == acctDubboResult ? RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc() : acctDubboResult.getResult().getRespDesc());
            mchntDiscountSurplusQuotaQueryResponse.setRespCode(null == acctDubboResult ? RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode() : acctDubboResult.getResult().getRespCode());
            bizResponse.setResult(mchntDiscountSurplusQuotaQueryResponse);
            return bizResponse;
        } else {
            if (null != acctDubboResult.getResult().getMcStatInfoList()) {
                List<MchntFeeDiscountMcStatInfoBean> mcStatInfoList = acctDubboResult.getResult().getMcStatInfoList();
                mcStatInfoList = mcStatInfoList.stream().filter(item -> item.getMchntNo().equals(mchntNo)).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(mcStatInfoList)) {
                    MchntFeeDiscountMcStatInfoBean mchntFeeDiscountMcStatInfo = mcStatInfoList.get(0);
                    mchntDiscountSurplusQuotaQueryResponse.setSurplusQuotaAmt(mchntFeeDiscountMcStatInfo.getAvailAmt());
                }
            }
        }
        bizResponse.setResult(mchntDiscountSurplusQuotaQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntDiscountSurplusQuotaQueryResponse mchntDiscountSurplusQuotaQueryResponse = (MchntDiscountSurplusQuotaQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntDiscountSurplusQuotaQueryResponse);
    }

}
